package cn.jarkata.commons.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * 数字类转换工具
 */
public final class NumberUtils {

    /**
     * 格式化BigDecimal为指定位数
     *
     * @param value    字符串
     * @param newScale 小数点保留位数
     * @return 格式化后的值
     */
    public static String formatDecimal(String value, int newScale) {
        if (StringUtils.isBlank(value)) {
            return "";
        }
        try {
            BigDecimal decimal = new BigDecimal(value);
            return decimal.setScale(newScale, RoundingMode.HALF_DOWN).toPlainString();
        } catch (Exception ex) {
            return "";
        }
    }

    public static String formatDecimal(Long value, int newScale) {
        if (Objects.isNull(value)) {
            return "";
        }
        try {
            BigDecimal decimal = new BigDecimal(Objects.toString(value));
            return decimal.movePointLeft(newScale).setScale(newScale, RoundingMode.HALF_DOWN).toPlainString();
        } catch (Exception ex) {
            return "";
        }
    }

    /**
     * 将BigDecimal转换为Long
     *
     * @param decimal  对象
     * @param newScale 小数位数
     * @return long值
     */
    public static long toLong(BigDecimal decimal, int newScale) {
        if (Objects.isNull(decimal)) {
            return 0;
        }
        if (newScale < 2) {
            newScale = 2;
        }
        return decimal.movePointRight(newScale).longValue();
    }

    /**
     * 字符串转换为指定小数位数的BigDecimal
     *
     * @param value    值
     * @param newScale 保留位数
     * @return BigDecimal对象
     */
    public static BigDecimal toDecimal(String value, int newScale) {
        try {
            BigDecimal bigDecimal = new BigDecimal(value);
            return bigDecimal.setScale(newScale, RoundingMode.HALF_UP);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 转换为BigDecimal对象
     *
     * @param obj        对象
     * @param defaultVal 默认值
     * @return BigDecimal对象
     */
    public static BigDecimal toDecimal(Object obj, BigDecimal defaultVal) {
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        }
        String str = Objects.toString(obj, null);
        if (Objects.isNull(str)) {
            return defaultVal;
        }
        str = StringUtils.trimToEmpty(str);
        try {
            return new BigDecimal(str);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static Long toLong(Object obj, Long defaultVal) {
        if (obj instanceof Long) {
            return (Long) obj;
        }
        String str = Objects.toString(obj, null);
        str = StringUtils.trimToEmpty(str);
        if (StringUtils.isBlank(str)) {
            return defaultVal;
        }

        try {
            return Long.parseLong(str);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static Integer toInt(Object obj, Integer defaultVal) {
        if (obj instanceof Integer) {
            return (Integer) obj;
        }
        String str = Objects.toString(obj, null);
        str = StringUtils.trimToEmpty(str);
        if (StringUtils.isBlank(str)) {
            return defaultVal;
        }
        try {
            return Integer.parseInt(str);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static Float toFloat(Object obj, Float defaultVal) {
        if (obj instanceof Float) {
            return (Float) obj;
        }
        String str = Objects.toString(obj, null);
        str = StringUtils.trimToEmpty(str);
        if (StringUtils.isBlank(str)) {
            return defaultVal;
        }
        try {
            return Float.parseFloat(str);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static Double toDouble(Object obj, Double defaultVal) {
        if (obj instanceof Double) {
            return (Double) obj;
        }
        String str = Objects.toString(obj, null);
        str = StringUtils.trimToEmpty(str);
        if (StringUtils.isBlank(str)) {
            return defaultVal;
        }
        try {
            return Double.parseDouble(str);
        } catch (Exception ex) {
            return defaultVal;
        }
    }
}